<?php

namespace plugin\ycMassage\app\admin\controller;

use app\common\builder\ListBuilder;
use plugin\ycMassage\app\BaseController;
use plugin\ycMassage\app\model\MassageOrders;
use plugin\ycMassage\app\model\MassageOrdersSub;
use plugin\ycMassage\utils\enum\OrdersState;
use plugin\ycMassage\utils\enum\OrdersType;
use plugin\ycMassage\utils\enum\PayType;
use think\facade\Db;
use think\Request;

class OrdersSubController extends BaseController
{
    public function indexGetTable(Request $request)
    {
        $limit   = $request->get('limit', 20);
        $builder = new ListBuilder;
        $builder->pageConfig([
            'pageSize' => $limit
        ]);
        $builder->addScreen('trade', 'input', '订单号');
        $builder->addScreen('name', 'input', '下单人');
        $builder->addScreen('mobile', 'input', '下单手机号');
        $builder->addScreen('technician', 'input', '技师');
        $builder->addScreen('state', 'select', '状态', [
            'options' => OrdersState::getOptions()
        ]);
        $builder->addScreen('start_time', 'input', '开始时间', [
            'type' => 'date'
        ]);
        $builder->addScreen('end_time', 'input', '结束时间', [
            'type' => 'date'
        ]);
        $builder->addColumn('id', '序号', [
            'width' => 90,
        ])
            ->addColumn('title', '服务项目详情', [
                'minWidth' => 200,
            ])
            ->addColumn('name', '下单人', [
                'width' => 160,
            ])
            ->addColumn('mobile', '下单手机号', [
                'width' => 160,
            ])
            ->addColumn('technician_nickname', '技师', [
                'width' => 160,
            ])
            ->addColumn('origin_price', '原价', [
                'width' => 100,
            ])
            ->addColumn('price', '实付', [
                'width' => 100,
            ])
            ->addColumn('unit_price', '单价', [
                'width' => 100,
            ])
            ->addColumn('overtime_price', '加钟价', [
                'width' => 100,
            ])
            ->addColumn('material_price', '材料费', [
                'width' => 100,
            ])
            ->addColumn('coupon_money', '优惠', [
                'width' => 100,
            ])
            ->addColumn('num', '服务数量', [
                'width' => 100,
            ])
            ->addColumn('trade', '订单号', [
                'minWidth' => 200,
            ])
            ->addColumnEle('state', '状态', [
                'width' => 100,
                'params' => [
                    'type' => 'tags',
                    'options' => OrdersState::dictOptions(),
                    'style' => OrdersState::dictOptions('style'),
                ],
            ])
            ->addColumn('create_time', '下单时间', [
                'width' => 160,
            ]);
        return $this->successRes($builder->create());
    }
    public function index(Request $request)
    {
        $page = $request->get('page', 1);
        $limit = $request->get('limit', 20);
        $where = [
            ['os.saas_appid', '=', $request->saas_appid]
        ];
        $trade = $request->get('trade');
        if ($trade) {
            $where[] = ['o.trade', 'like', '%' . $trade . '%'];
        }
        $uid = $request->get('uid');
        if ($uid) {
            $where[] = ['os.uid', '=', $uid];
        }
        $state = $request->get('state');
        if ($state !== null) {
            $where[] = ['os.state', '=', $state];
        }
        $start_time = $request->get('start_time');
        if ($start_time) {
            $where[] = ['os.create_time', '>=', $start_time . ' 00:00:00'];
        }
        $end_time = $request->get('end_time');
        if ($end_time) {
            $where[] = ['os.create_time', '<=', $end_time . ' 23:59:59'];
        }
        $type = $request->get('type');
        if ($type) {
            $where[] = ['os.orders_type', '=', $type];
        }
        $filed = [
            'o.trade',
            'os.*',
            'address.name,address.mobile',
            'ut.nickname as technician_nickname',
            's.title'
        ];
        $MassageOrdersSubObj = MassageOrdersSub::alias('os')->where($where);
        $technician = $request->get('technician');
        if ($technician) {
            $where[] = ['t.name|t.mobile', 'like', "%{$technician}%"];
            $MassageOrdersSubObj->join('MassageUserTechnician t', 't.uid=os.technician_id');
        }
        $MassageOrdersSubObj->join('MassageUser ut', 'ut.id=os.technician_id');
        $name = $request->get('name');
        if ($name) {
            $where[] = ['address.name', 'like', "%{$name}%"];
        }
        $mobile = $request->get('mobile');
        if ($mobile) {
            $where[] = ['address.mobile', 'like', "%{$mobile}%"];
        }
        $MassageOrdersSubObj->join('MassageOrders o', 'o.id = os.orders_id');
        $MassageOrdersSubObj->join('MassageOrdersAddress address', 'address.orders_id = os.orders_id');
        $MassageOrdersSubObj->join('MassageService s', 's.id = os.service_id');
        $data = $MassageOrdersSubObj->order('os.id desc')
            ->field($filed)
            ->paginate([
                'list_rows' => $limit,
                'page' => $page
            ])->each(function ($item) {
                if ($item->technician_nickname) {
                    $item->technician_nickname = base64_decode($item->technician_nickname);
                }
            });
        return $this->successRes($data);
    }
}
